<template>
  <div class="modal">
    <div class="mask" @click="changeIsShowLoginModal(false)"></div>
    <div class="LoginBox">
      <!-- 关闭按钮 -->
      <span class="close" @click="changeIsShowLoginModal(false)"></span>
      <!-- 导航 -->
      <div class="nav">
        <a :class="{ active: isShow }" href="#" @click="isShow = true"
          >手机号码登录</a
        >
        |
        <a :class="{ active: !isShow }" href="#" @click="showQRcode"
          >微信扫码登录</a
        >
      </div>
      <!-- 内容 -->
      <div class="conetnt">
        <!-- 手机号码内容 -->
        <div v-show="isShow" class="phoneCentent">
          <div class="row">
            <input
              type="text"
              class="ipt"
              placeholder="请输入手机号码"
              v-model="phoneNumber"
            />
          </div>
          <div class="row">
            <slide-verify
              :l="42"
              :r="20"
              :w="362"
              :h="140"
              @success="onSuccess"
              @fail="onFail"
              @refresh="onRefresh"
              :style="{ width: '100%' }"
              class="slide-box"
              ref="slideBlock"
              :slider-text="msg"
              :imgs="arr"
              :accuracy="accuracy"
            ></slide-verify>
          </div>
          <div class="row">
            <input
              type="text"
              class="ipt"
              placeholder="请输入短信验证码"
              v-model="value"
            />
            <!-- <button class="getbtn" @click="getCode">获取验证码</button> -->
            <button class="getbtn" v-show="showCount" @click="getCode">
              获取验证码
            </button>
            <button class="getbtn" v-show="!showCount">
              {{ count + "s" }}
            </button>
          </div>
          <button class="loginbtn" @click="submitFn">登录</button>
        </div>
        <!-- 微信扫码内容 -->
        <div v-show="!isShow" class="qrCentent" id="weixin"></div>
      </div>
    </div>
  </div>
</template>

<script>
import { validataPhoneNumber } from "@/utils/index";
import { sendSMSAPI, PhoneReginAPI, BindingAPI } from "@/request/api";
import { mapMutations, mapActions } from "vuex";
export default {
  name: "JifenThreeLogin",

  data() {
    return {
      isShow: true,
      msg: "向右滑动",
      arr: [
        require("../assets/img/integral-01.9386d4bf.png"),
        require("../assets/img/integral-02.150d92a1.png"),
        require("../assets/img/integral-03.9870f3f1.png"),
        require("../assets/img/integral-04.afadcbdf.png"),
      ],
      phoneNumber: "",
      count: 59,
      showCount: true,
      accuracy: 100,
      // 手机验证码
      value: "",
    };
  },
  methods: {
    ...mapMutations({
      changeIsShowLoginModal: "showModal/changeIsShowLoginModal",
      changeIsLogined: "showStatus/changeIsLogined",
    }),
    ...mapActions({
      asyncChangeIsShowToast: "showToast/asyncChangeIsShowToast",
      asyncchangeUserInfo: "userInfo/asyncchangeUserInfo",
    }),
    // 拼图成功
    onSuccess(times) {
      let ms = (times / 1000).toFixed(1);
      this.msg = "login success, 耗时 " + ms + "s";
    },
    // 拼图失败
    onFail() {
      this.onRefresh(); // 重新刷新拼图
    },
    // 拼图刷新
    onRefresh() {
      this.msg = "再试一次";
    },
    // 倒计时
    daojishi() {
      this.showCount = false;
      let tiemr = setInterval(() => {
        this.count--;
        if (this.count <= 0) {
          this.count = 59;
          this.showCount = true;
          clearInterval(tiemr);
        }
      }, 1000);
    },
    // 判断手机号码和滑块验证
    toverify() {
      if (!validataPhoneNumber(this.phoneNumber)) {
        this.asyncChangeIsShowToast({
          show: true,
          msg: "请输入手机号码",
          type: "info",
        });
        return;
      }
      // 判断滑块验证是否正确
      if (this.msg == "再试一次" || this.msg == "向右滑动") {
        this.asyncChangeIsShowToast({
          show: true,
          msg: "请移动滑块",
          type: "info",
        });
        return;
      }
      return true;
    },
    // 获取验证码
    async getCode() {
      // 判断手机号码和滑块验证是否正确
      if (!this.toverify()) return;
      //   发送请求
      let res = await sendSMSAPI({
        phone: this.phoneNumber,
      });
      console.log(res);
      // 判断倒计时是否发送成功,如果失败则返回,不继续执行  data.message
      if (!res) return;
      this.asyncChangeIsShowToast({
        show: true,
        msg: "发送成功",
        type: "success",
      });
      // 调用倒计时
      this.daojishi();
    },
    // 登录
    async submitFn() {
      //1.判断手机号码和滑块验证是否正确
      if (!this.toverify()) return;
      // 2.判断短信验证码     提示请输入短信验证码
      if (!this.value.trim())
        return this.asyncChangeIsShowToast({
          show: true,
          msg: "请输入短信验证码",
          type: "info",
        });
      // 3.发送请求  两种情况, 1.如果有uuid,发送微信绑定手机号请求,如果没有就是普通请求
      let uuid = localStorage.getItem("uuid");
      let res = null;
      if (uuid) {
        res = BindingAPI({
          verifyCode: this.value,
          phone: this.phoneNumber,
          uuid,
        });
      } else {
        res = await PhoneReginAPI({
          verifyCode: this.value,
          phone: this.phoneNumber,
        });
      }
      // 4.请求成功后本地存储token  localStorage.setItem("名称",数据)
      localStorage.setItem("x-auth-token", res["x-auth-token"]);
      // 5.登录状态栏及模态框的状态改变
      // 模态框
      this.changeIsShowLoginModal(false);
      // 购物车
      this.changeIsLogined(true);
      // 删除code
      this.$router.push(this.$route.path);
      // 删除uuid
      localStorage.removeItem("uuid");
      // 提示登录成功
      this.asyncChangeIsShowToast({
        show: true,
        msg: "登录成功",
        type: "success",
      });
      this.asyncchangeUserInfo();
    },
    // 微信扫码登录
    showQRcode() {
      this.isShow = false;
      //获取二维码 并展示
      // 申请微信登录二维码
      let _this = this;
      new WxLogin({
        id: "weixin",
        appid: "wx67cfaf9e3ad31a0d", // 这个appid要填死
        scope: "snsapi_login",
        // 扫码成功后重定向的接口
        redirect_uri: "https://sc.wolfcode.cn/cms/wechatUsers/shop/PC",
        // state填写编码后的url
        state: encodeURIComponent(
          window.btoa("http://127.0.0.1:8080" + _this.$route.path)
        ),
        // 调用样式文件
        href: "data:text/css;base64,LmltcG93ZXJCb3ggLnRpdGxlLA0KLmltcG93ZXJCb3ggLmluZm8gew0KICAgIGRpc3BsYXk6IG5vbmU7DQp9",
      });
    },
  },
};
</script>

<style lang="less" scoped>
.modal {
  width: 100%;
  height: 100%;
  position: fixed;
  top: 0;
  left: 0;
  .mask {
    width: 100%;
    height: 100%;
    position: relative;
    top: 0;
    left: 0;
    background: rgba(0, 0, 0, 0.5);
  }
  .LoginBox {
    width: 555px;
    height: 423px;
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    margin: auto;
    background: url("../assets/img/login-box-bg.png");
    .close {
      width: 22px;
      height: 22px;
      position: absolute;
      top: 10px;
      right: 60px;
      background: url("../assets/img/close.png");
    }
    .nav {
      text-align: center;
      padding-top: 30px;
      a {
        text-decoration: none;
        color: gray;
        cursor: pointer;
        &.active {
          color: black;
        }
      }
    }
    .conetnt {
      width: 350px;
      margin: auto;
      height: 280px;
      margin-top: 30px;
      .row {
        display: flex;
        margin-bottom: 20px;
        .ipt {
          flex: 1;
          height: 50px;
          outline: none;
          text-indent: 1em;
        }
        .getbtn {
          width: 80px;
          margin-left: 10px;
          background: #0a328e;
          color: white;
          border: none;
        }
      }
      .loginbtn {
        height: 50px;
        width: 100%;
        background: #0a328e;
        color: white;
        border: none;
      }
    }
    .qrCentent {
      text-align: center;
    }
  }
}

/deep/.slide-box {
  width: 100%;
  position: relative;
  box-sizing: border-box;
  canvas {
    position: absolute;
    left: 0;
    top: -120px;
    display: none;
    width: 100%;
    box-sizing: border-box;
  }
  .slide-verify-block {
    width: 85px;
    height: 136px;
  }
  .slide-verify-refresh-icon {
    top: -120px;
    display: none;
  }
  &:hover {
    canvas {
      display: block;
    }
    .slide-verify-refresh-icon {
      display: block;
    }
  }
}
</style>